package com.xhtt.modules.task.utils;

import com.alibaba.fastjson.JSON;
import com.xhtt.common.utils.DateUtils;
import com.xhtt.common.utils.RestResult;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import sun.security.provider.MD5;

import java.time.LocalDateTime;

@Component
public class RestUtils {

    @Value("${renren.wuliu.ship: http://www.yuntai.site:8888/innovate-api/v1/logistics/safetyPlusApi/updateCbzt}")
    private String url;
    /**
     * 想物流系统更新装卸船的lm任务状态
     *
     * @return
     */
    public RestResult updateShipLMStatus(String serial_no, LocalDateTime kbsj, LocalDateTime kssj, LocalDateTime jssj, LocalDateTime lbsj) {
        RestTemplate restTemplate = new RestTemplate();
        String url = this.url;
//        String url = "http://127.0.0.1:8088/app/sys/cfg/tube/sync";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        String serect = "safetyplus";
        String serect_key = DigestUtils.md5Hex(serect + serect);
        headers.add("serect", serect);
        headers.add("serect_key", serect_key);
        ShipLmModel pm = new ShipLmModel();
        pm.setSerial_no(serial_no);
        pm.setKbsj(null == kbsj ? "" : kbsj.format(DateUtils.dtf));
        pm.setKssj(null == kssj ? "" : kssj.format(DateUtils.dtf));
        pm.setJssj(null == jssj ? "" : jssj.format(DateUtils.dtf));
        pm.setLbsj(null == lbsj ? "" : lbsj.format(DateUtils.dtf));
//        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
//        map.add("serial_no", serial_no);
//        map.add("kbsj", null == kbsj ? "" : kbsj.format(DateUtils.dtf));
//        map.add("kssj", null == kssj ? "" : kssj.format(DateUtils.dtf));
//        map.add("jssj", null == jssj ? "" : jssj.format(DateUtils.dtf));
//        map.add("lbsj", null == lbsj ? "" : lbsj.format(DateUtils.dtf));

        HttpEntity<ShipLmModel> request = new HttpEntity<>(pm, headers);
        RestResult result;
        try {
            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
            String res = response.getBody();
            result = JSON.parseObject(res, RestResult.class);
        } catch (Exception e) {
            e.printStackTrace();
            result = new RestResult("400", "发送异常", "", JSON.toJSONString(pm));
        }
        if (!"0".equals(result.getCode())) {
            result.setSendInfo(JSON.toJSONString(pm));
        }
        return result;
    }

    public static void main(String[] args) {
        LocalDateTime d = LocalDateTime.now();
        RestUtils u = new RestUtils();
        u.updateShipLMStatus("2143wqr23r", d, null, null, null);
    }
}
